package com.zara.store.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.DateConverter;

public class EnvtHelper {

	public EnvtVO createEnvtFromXml(File file) throws FileNotFoundException,
			IOException {
		EnvtVO envtVO1;
		XStream xstream = createXstream();
		FileInputStream is = null;
		try {
			is = new FileInputStream(file);
			envtVO1 = (EnvtVO) xstream.fromXML(is);
			return envtVO1;
		} finally {
			if (is != null) {
				is.close();
			}
		}
	}

	public EnvtVO createEnvtFromXml(String xml) {
		XStream xstream = createXstream();
		EnvtVO envtVO1 = (EnvtVO) xstream.fromXML(xml);
		return envtVO1;
	}
	
	private XStream createXstream() {
		XStream xstream = new XStream();

		xstream.addDefaultImplementation(ArrayList.class, Collection.class);
		// xstream.addDefaultImplementation(java.sql.Date.class,
		// java.util.Date.class);
		xstream.registerConverter(new DateConverter("yyyy-MM-dd HH:mm:ss.SSS", new String[] { }));

		xstream.alias("envt", EnvtVO.class);
		xstream.alias("product", ProductVO.class);
		return xstream;
	}

}
